package net.babypai.utils;

import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import android.util.Log;

public class IIYaClient {

	public IIYaClient(){
		
	}
	public String doGet(String url, Map<String, String> addtionalParams){
		String result = null;
		url = UrlUtils.buildUrlByQueryStringMapAndBaseUrl(url, addtionalParams);
		System.out.println("url------->" + url);
		HttpGet getRequest = new HttpGet(url);
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse response = null;
		try{
			response = httpClient.execute(getRequest);
			result = ApacheUtils.parseStringFromEntity(response.getEntity());
		}
		catch (Exception e) {
			e.printStackTrace();
			log(e.toString());
			result = "{\"msg\":0,\"timestamp\":200}";
		}
		return result;
	}
	public String doPost(String url, List<NameValuePair> nameValuePair){
		String result = null;
		try {
			System.out.println("url------->" + url);
            HttpClient httpClient = new DefaultHttpClient();  
            HttpPost httpPost = new HttpPost(url);
            System.out.println("utl----->" + url);
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
            HttpResponse response = httpClient.execute(httpPost);  
            result = ApacheUtils.parseStringFromEntity(response.getEntity());
        } catch (Exception e) {  
        	e.printStackTrace();
        	log(e.toString());
        	result = "{\"msg\":0,\"timestamp\":200}";
        }
		return result;
	}
	void log(String msg) {
		Log.i("iiya", "IIYaClient--" + msg);
	}
}
